<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    setTimeout(() => {
        console.log('==');

    })
    let http = new XMLHttpRequest()

    http.open('post', 'http://localhost:3001/test?a=1&b=2', true) //true表示异步  false表示同步
    http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    http.onreadystatechange = function () {
        if (http.readyState == 4 && http.status == 200) {

            console.log(http, 'http');
            let data = http.responseText
            console.log(data, 'data');
        }
    }


    var data = JSON.stringify({
        name: "John Doe",
        email: "john.doe@example.com",
        message: "Hello, World!"
    });

    // 发送请求并传递数据
    document.cookie = 'aa=200'
    http.send(data);
    /* 封装ajax */
    function ajax(option = {}) {
        return new Promise((resolve, reject) => {
            let xhr = new XMLHttpRequest()



            // xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8")
            xhr.onreadystatechange = function () {
                console.log(xhr.readyState, xhr);

                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        resolve(xhr.responseText)
                    }
                }
            }
            xhr.open(option.method, option.url, true)
            xhr.setRequestHeader('Content-Type', 'application/json');

            xhr.setRequestHeader('Authorization', 'Bearer ')

            data ? xhr.send() : xhr.send(data)
        })
    }
    // ajax({ url: 'http://localhost:3001/jsonp', method: 'get' }).then((res) => {
    //     console.log(res, 'res');

    // })
    // setInterval(() => {
    ajax({ url: 'http://localhost:3001/xx', method: 'get' }).then((res) => console.log(res, 'res'))
    // }, 1000)

</script>

</html>